package com.superzqb.asus.superzqbFragment.superzqbJobShop.Shop;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.superzqb.asus.superzqb.R;
import com.superzqb.asus.superzqbFragment.superzqbJobShop.Bean.CampusGoodsOderBean;
import com.superzqb.asus.superzqbFragment.superzqbJobShop.Shop.dialog.CommonDialog;
import com.superzqb.asus.Api.CampusShopApi;
import com.superzqb.asus.superzqbUtil.CustomerKeyboard;
import com.superzqb.asus.superzqbUtil.GeneralFun;
import com.superzqb.asus.superzqbUtil.LoadDialog;
import com.superzqb.asus.superzqbUtil.PasswordEditText;
import com.superzqb.asus.superzqbUtil.Url;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;


/**
 * Created by asus on 2017/2/1.
 */

public class CampusGoodsSubmit extends Activity implements CustomerKeyboard.CustomerKeyboardClickListener,
        PasswordEditText.PasswordFullListener {
    private int gid, aa, bb, pc;
    private PopupWindow v;
    String cc,total;
    private Dialog loadingDialog;

    private ImageButton back, back_;
    private Button submit, submit_c;
    private TextView uname, phone, adress, sname, gname, type, price, count, sum, sum1,price_c;
    private ImageView img, minus, plus;
    private EditText number;
    CampusGoodsOderBean detail;
    CampusGoodsOderBean.AdressInfo ad;
    CampusGoodsOderBean.CampusGoodsInfo gd;

    SharedPreferences sp;
    CustomerKeyboard mCustomerKeyboard;
    PasswordEditText mPasswordEditText;
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.campus_shop_goods_submit);
        initView();
        loadingDialog= LoadDialog.getInstance(context,"正在加载");
        loadingDialog.show();

        Intent intent = this.getIntent();
        /*gid=Integer.valueOf(sp.getString("gid",null));
        aa=Integer.valueOf(sp.getString("aa",null));
        bb=Integer.valueOf(sp.getString("bb",null));*/
        gid = Integer.valueOf(intent.getStringExtra("gid"));
        aa = Integer.valueOf(intent.getStringExtra("aa"));
        bb = Integer.valueOf(intent.getStringExtra("bb"));
        cc = intent.getStringExtra("count");
        pc = Integer.valueOf(intent.getStringExtra("sum"));

        getData();

    }

    private void initView() {
      /*  sp=getSharedPreferences("submit",MODE_PRIVATE);*/
      context=CampusGoodsSubmit.this;
        sp=getSharedPreferences("uinfo", Context.MODE_PRIVATE);
        back = (ImageButton) findViewById(R.id.back);//返回
        uname = (TextView) findViewById(R.id.uname);//收货人
        phone = (TextView) findViewById(R.id.phone);//电话
        adress = (TextView) findViewById(R.id.adress);//地址
        sname = (TextView) findViewById(R.id.sname);//店铺名
        img = (ImageView) findViewById(R.id.img);//商品图片
        gname = (TextView) findViewById(R.id.gname);//商品名
        type = (TextView) findViewById(R.id.type);//商品类别
        price = (TextView) findViewById(R.id.price);//商品价格
        count = (TextView) findViewById(R.id.count);//商品数量
        minus = (ImageView) findViewById(R.id.minus);//减少商品
        number = (EditText) findViewById(R.id.number);//商品数量
        plus = (ImageView) findViewById(R.id.plus);//增加商品
        sum = (TextView) findViewById(R.id.sum);//小计
        sum1 = (TextView) findViewById(R.id.sum1);//合计
        submit = (Button) findViewById(R.id.subBtn);//提交
        back.setOnClickListener(listen);
        minus.setOnClickListener(listen);
        plus.setOnClickListener(listen);
        submit.setOnClickListener(listen);
    }

    protected View.OnClickListener listen = new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.back:
                    finish();
                    break;
                case R.id.minus:
                    int num = Integer.parseInt(number.getText().toString());
                    num--;
                    if (num > 1 && num < pc) {
                        minus.setBackground(getResources().getDrawable(R.mipmap.minus));
                        plus.setBackground(getResources().getDrawable(R.mipmap.plus));
                    } else if (num == 0 && 1 == pc) {
                        minus.setBackground(getResources().getDrawable(R.mipmap.minus_dis));
                        plus.setBackground(getResources().getDrawable(R.mipmap.plus_dis));
                        num++;
                    } else if (num == 0 && pc != 1) {
                        minus.setBackground(getResources().getDrawable(R.mipmap.minus_dis));
                        plus.setBackground(getResources().getDrawable(R.mipmap.plus));
                        num++;
                    }
                    number.setText(num + "");
                    count.setText("x" + num);
                    total=new GeneralFun().stringTodouble(num * Double.parseDouble(gd.getPrice())+"");
                    sum.setText("共" + num + "件商品,小计:" +total + "元");
                    sum1.setText("合计:" +total + "元");
                    /*if(num>1){
                        number.setText(--num+"");
                        if(num==1)
                            minus.setBackground(getResources().getDrawable(R.mipmap.minus_dis));
                        else
                            minus.setBackground(getResources().getDrawable(R.mipmap.minus));
                    }else{
                        minus.setBackground(getResources().getDrawable(R.mipmap.minus_dis));
                    }*/
                    break;
                case R.id.plus:
                    int num1 = Integer.parseInt(number.getText().toString());
                    num1++;
                    if (num1 > 1 && num1 < pc) {
                        minus.setBackground(getResources().getDrawable(R.mipmap.minus));
                        plus.setBackground(getResources().getDrawable(R.mipmap.plus));
                    } else if (num1 == 2 && 1 == pc) {
                        minus.setBackground(getResources().getDrawable(R.mipmap.minus_dis));
                        plus.setBackground(getResources().getDrawable(R.mipmap.plus_dis));
                        num1--;
                    } else if (num1 > pc && num1 != 1) {
                        minus.setBackground(getResources().getDrawable(R.mipmap.minus));
                        plus.setBackground(getResources().getDrawable(R.mipmap.plus_dis));
                        num1--;
                    } else if (num1 == 2 && num1 < pc) {
                        minus.setBackground(getResources().getDrawable(R.mipmap.minus_dis));
                        plus.setBackground(getResources().getDrawable(R.mipmap.plus));
                        num1--;
                    }
                    number.setText(num1 + "");
                    count.setText("x" + num1);
                    total=new GeneralFun().stringTodouble(num1 * Double.parseDouble(gd.getPrice())+"");
                    sum.setText("共" + num1 + "件商品,小计:" + total + "元");
                    sum1.setText("合计:" + total + "元");
                    /*if(num1>1&&num1<pc) {
                        minus.setBackground(getResources().getDrawable(R.mipmap.minus));
                        plus.setBackground(getResources().getDrawable(R.mipmap.plus));
                    }else if(pc==1){
                        minus.setBackground(getResources().getDrawable(R.mipmap.minus_dis));
                        plus.setBackground(getResources().getDrawable(R.mipmap.minus_dis));
                    }
                        minus.setBackground(getResources().getDrawable(R.mipmap.minus_dis));
                    number.setText(num1+"");
                    if(num1>=pc){
                        plus.setBackground(getResources().getDrawable(R.mipmap.plus_dis));
                        number.setText(pc+"");
                    }*/
                    break;
                case R.id.subBtn:
                    setBackgroundAlpha(0.5f);
                    showDetail();
                   /* Intent intent=new Intent();
                    new GeneralFun().ata(CampusGoodsSubmit.this,CampusGoodsPayDetail.class,intent);*/
                    break;
                /*确认popupwindow里面的 控件*/
                case R.id.delete_black:
                    closeDetail(getV());
                    break;
                case R.id.commit_c:
                     final CommonDialog.Builder builder = new CommonDialog.Builder(CampusGoodsSubmit.this).fullWidth().fromBottom()
                             .setCancelable(false).setView(R.layout.dialog_customer_keyboard);
                    builder.setOnClickListener(R.id.delete_dialog, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            builder.dismiss();
                        }
                    });
                    //closeDetail(getV());
                    builder.create().show();
                    mCustomerKeyboard = builder.getView(R.id.custom_key_board);
                    mCustomerKeyboard.setOnCustomerKeyboardClickListener(CampusGoodsSubmit.this);
                    mPasswordEditText = builder.getView(R.id.password_edit_text);
                    mPasswordEditText.setOnPasswordFullListener(CampusGoodsSubmit.this);
                    break;
                default:
                    break;
            }
        }
    };

    public void getData() {
        Retrofit retrofit = new GeneralFun().gsonCreate();
        CampusShopApi api = retrofit.create(CampusShopApi.class);
        Call<CampusGoodsOderBean> call = api.subGoodsOrder(gid,Integer.valueOf(sp.getString("uid",null)), aa, bb);
        call.enqueue(new Callback<CampusGoodsOderBean>() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onResponse(Call<CampusGoodsOderBean> call, Response<CampusGoodsOderBean> response) {
                //Toast.makeText(CampusGoodsSubmit.this,"aa",Toast.LENGTH_SHORT).show();
                if (response.isSuccessful()) {
                    detail = response.body();
                    ad = detail.getAddress();
                    gd = detail.getGoods();
                    uname.setText("收货人:" + ad.getName());
                    phone.setText(ad.getPhone());
                    adress.setText("收货地址:" + ad.getAddress());
                    sname.setText(gd.getU_id());
                    Picasso.with(CampusGoodsSubmit.this).load(new Url().getImg_url() + gd.getImg()).into(img);
                    gname.setText(gd.getGoodsname());
                    String aa ;
                    if (gd.getFlag().trim().equals("1")) {
                        aa = gd.getType().get(0) + ":" + gd.getContent().get(0) + "," + gd.getType().get(1) + ":" + gd.getContent().get(1);
                    } else {
                        aa = gd.getType().get(0) + ":" + gd.getContent().get(0);
                    }
                    type.setText(aa);
                    price.setText(gd.getPrice());
                    count.setText("x" + cc);
                    number.setText(cc);
                    if (Integer.valueOf(cc) > 1 && Integer.valueOf(cc) < pc) {
                        minus.setBackground(getResources().getDrawable(R.mipmap.minus));
                        plus.setBackground(getResources().getDrawable(R.mipmap.plus));
                    } else if (Integer.valueOf(cc) == 1 && Integer.valueOf(cc) == pc) {
                        minus.setBackground(getResources().getDrawable(R.mipmap.minus_dis));
                        plus.setBackground(getResources().getDrawable(R.mipmap.plus_dis));
                    } else if (Integer.valueOf(cc) == pc && Integer.valueOf(cc) != 1) {
                        minus.setBackground(getResources().getDrawable(R.mipmap.minus));
                        plus.setBackground(getResources().getDrawable(R.mipmap.plus_dis));
                    } else if (Integer.valueOf(cc) == 1 && Integer.valueOf(cc) < pc) {
                        minus.setBackground(getResources().getDrawable(R.mipmap.minus_dis));
                        plus.setBackground(getResources().getDrawable(R.mipmap.plus));
                    }
                    total=new GeneralFun().stringTodouble(Integer.valueOf(cc) * Double.parseDouble(gd.getPrice())+"");
                    sum.setText("共" + cc + "件商品,小计:" +total + "元");
                    sum1.setText("合计:" + total + "元");
                }
                loadingDialog.dismiss();

            }

            @Override
            public void onFailure(Call<CampusGoodsOderBean> call, Throwable throwable) {
                loadingDialog.dismiss();
                Toast.makeText(CampusGoodsSubmit.this, "error", Toast.LENGTH_SHORT).show();
            }
        });
    }

    /*设置popupWindow commit*/
    private void showDetail() {
        View detailView = View.inflate(this, R.layout.campus_shop_goods_pay_detail, null);
        int width = getResources().getDisplayMetrics().widthPixels;//获取屏幕宽度
        int height = getResources().getDisplayMetrics().heightPixels;//获取屏幕高度
        PopupWindow newView = new PopupWindow(detailView, width, height / 72*41 );//创建一个popupwindow
        newView.setAnimationStyle(R.style.campus_goods_ani);//设置window滑出动画
        /*点击空白处消失window*/
        newView.setFocusable(true);//设置window可以获取焦点
        newView.setOutsideTouchable(false);//设置不允许在外点击消失
        //newView.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
        newView.showAtLocation(detailView, Gravity.BOTTOM, 0, 0);
        newView.setOnDismissListener(new popWindowDismiss(1f));
        initPopView(detailView);//绑定popWindow里面控件的事件
        setV(newView);
    }

    private void initPopView(View view) {
        back_ = (ImageButton) view.findViewById(R.id.delete_black);
        submit_c = (Button) view.findViewById(R.id.commit_c);
        price_c=(TextView)view.findViewById(R.id.price_c);
        price_c.setText(total+"元");
        back_.setOnClickListener(listen);
        submit_c.setOnClickListener(listen);
    }


    @Override
    public void click(String number) {
        mPasswordEditText.addPassword(number);

    }

    @Override
    public void delete() {
        mPasswordEditText.deleteLastPassword();
    }

    @Override
    public void passwordFull(String password) {
        Toast.makeText(CampusGoodsSubmit.this, "密码填充完毕：" + password, Toast.LENGTH_SHORT).show();
    }




    /*把半透明背景还原*/
    class popWindowDismiss implements PopupWindow.OnDismissListener {
        float value;

        public popWindowDismiss(float a) {
            this.value = a;
        }

        @Override
        public void onDismiss() {
            setBackgroundAlpha(value);
        }
    }

    //设置透明度
    public void setBackgroundAlpha(float backgroundAlpha) {
        WindowManager.LayoutParams l = getWindow().getAttributes();
        l.alpha = backgroundAlpha;
        getWindow().setAttributes(l);
    }

    private void closeDetail(PopupWindow view) {
        view.dismiss();
        new popWindowDismiss(1f);
    }

    public PopupWindow getV() {
        return v;
    }

    public void setV(PopupWindow v) {
        this.v = v;
    }
}
